/*
 * @(#)XfjOutputMemFunc.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//	GENERADOR DEL FICHERO "OutputMembershipFunction.java"	//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

package xfuzzy.xfj;

import java.io.*;

public class XfjOutputMemFunc {

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			MIEMBROS PRIVADOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 private String eol = System.getProperty("line.separator", "\n");
 private File dir;
 private String pkgname;

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			METODOS CONSTANTES			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 public static final String create(File dir, String pkgname) {
  XfjOutputMemFunc creator = new XfjOutputMemFunc(dir,pkgname);
  creator.createFile();
  return creator.getMessage();
 }

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			   CONSTRUCTOR				//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 public XfjOutputMemFunc(File dir, String pkgname) {
  this.dir = dir;
  this.pkgname = pkgname;
 }

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			METODOS PUBLICOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 //-------------------------------------------------------------//
 // Obtiene el nombre del fichero creado			//
 //-------------------------------------------------------------//

 private String getMessage() {
  File file = new File(dir,"OutputMembershipFunction.java");
  return file.getAbsolutePath();
 }

 //-------------------------------------------------------------//
 // Genera el fichero "OutputMembershipFunction.java"		//
 //-------------------------------------------------------------//

 public void createFile() {
  File file = new File(dir,"OutputMembershipFunction.java");

  String heading[] = getHeading();
  String source[] = getSource();

  String code = "";
  for(int i=0; i<heading.length; i++) code += heading[i]+eol;
  code += getPackage()+eol+eol;
  for(int i=0; i<source.length; i++) code += source[i]+eol;

  byte[] buf = code.getBytes();
  try {
   OutputStream stream = new FileOutputStream(file);
   stream.write(buf);
   stream.close();
  }
  catch (IOException e) {}
 }

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			METODOS PRIVADOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 //-------------------------------------------------------------//
 // Genera el codigo de cabecera				//
 //-------------------------------------------------------------//

 private String[] getHeading() {
  String source[] = {
   "//++++++++++++++++++++++++++++++++++++++++++++++++++++++//",
   "//                                                      //",
   "// Class:  OutputMembershipFunction                     //",
   "//                                                      //",
   "// Author: Automatically generated by Xfuzzy            //",
   "//                                                      //",
   "// Description: Membership function of an output        //",
   "//              variable                                //",
   "//                                                      //",
   "//++++++++++++++++++++++++++++++++++++++++++++++++++++++//",
   "" };

  return source;
 }

 //-------------------------------------------------------------//
 // Genera el codigo del paquete				//
 //-------------------------------------------------------------//

 private String getPackage() {
  if(pkgname != null && pkgname.length()>0) return "package "+this.pkgname+";";
  return "";
 }

 //-------------------------------------------------------------//
 // Genera el codigo de la clase "OutputMembershipFunction"	//
 //-------------------------------------------------------------//

 private String[] getSource() {
  String source[] = {
"public class OutputMembershipFunction implements MembershipFunction {",
" public Conclusion[] conc;",
" public double[] input;",
" private OperatorSet op;",
"",
" public OutputMembershipFunction() {",
"  this.conc = new Conclusion[0];",
" }",
"",
" public void set(int size, OperatorSet op, double[] input) {",
"  this.input = input;",
"  this.op = op;",
"  this.conc = new Conclusion[size];",
" }",
"",
" public void set(int pos, double dg, InputMembershipFunction imf) {",
"  this.conc[pos] = new Conclusion(dg,imf,op);",
" }",
"",
" public double compute(double x) {",
"  double dom = conc[0].compute(x);",
"  for(int i=1; i<conc.length; i++) dom = op.also(dom,conc[i].compute(x));",
"  return dom;",
" }",
"",
" public double defuzzify() {",
"  return op.defuz(this);",
" }",
"",
" public double min() {",
"  return conc[0].min();",
" }",
"",
" public double max() {",
"  return conc[0].max();",
" }",
"",
" public double step() {",
"  return conc[0].step();",
" }",
"",
" public boolean isDiscrete() {",
"  for(int i=0; i<conc.length; i++) if(!conc[i].isSingleton()) return false;",
"  return true;",
" }",
" ",
" public double[][] getDiscreteValues() {",
"  double[][] value = new double[conc.length][2];",
"  for(int i=0; i<conc.length; i++) {",
"   value[i][0] = conc[i].param(0);",
"   value[i][1] = conc[i].degree();",
"  }",
"  return value;",
" }",
"",
"}" };

  return source;
 }
}
